﻿using Microsoft.Extensions.Localization;
using PHMEE.Localization;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Volo.Abp;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;

namespace PHMEE
{
    public class MainenanceAdviceManager : DomainService, IMainenanceAdviceManager
    {
        private readonly IStringLocalizer<PHMEEResource> L;
        private readonly IMainenanceAdviceRepository maintenanceRepository;

        public MainenanceAdviceManager(IStringLocalizer<PHMEEResource> L, IMainenanceAdviceRepository maintenanceRepository)
        {
            this.L = L;
            this.maintenanceRepository = maintenanceRepository;
        }
        public async Task<MainenanceAdvice> CreateMainenanceAdvice(string code)
        {
            var existingMainenanceAdvice = await maintenanceRepository.FirstOrDefaultAsync(item => item.Code == code);
            if (existingMainenanceAdvice != null)
            {
                throw new UserFriendlyException(L[PHMEEDomainErrorCodes.MainenanceAdviceCodeAlreadyExist], PHMEEDomainErrorCodes.MainenanceAdviceCodeAlreadyExist).WithData("Code", code);
            }
            var id = GuidGenerator.Create();
            return new MainenanceAdvice(id, code);
        }
    }
}
